package Year22_Month06.day25;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

public class 组合总和2 {
    List<List<Integer>> list = new ArrayList<>();
    LinkedList<Integer> path = new LinkedList<>();
    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
        Arrays.sort(candidates);
        backstcking(candidates,0,target,0);
        return list;
    }

    public void backstcking(int[] candidates,int startIndex,int target,int sum){
        if(sum == target){
            list.add(new ArrayList(path));
            return;
        }
        for(int i = startIndex;i < candidates.length;i++){
            if ( i > startIndex && candidates[i] == candidates[i - 1] ) {
                continue;
            }
            if(sum + candidates[i] > target) break;
            path.add(candidates[i]);
            backstcking(candidates,i + 1,target,sum + candidates[i]);
            path.removeLast();
        }
    }
}
